package com.whfc.ms.api.emp.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.AppTrainingQuestionsDTO;
import com.whfc.emp.param.AppTrainingQuestionAddParam;
import com.whfc.emp.param.AppTrainingQuestionEditParam;
import com.whfc.emp.param.AppTrainingQuestionListParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsTrainingQuestionsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 题库管理-问题
 *
 * @author realm
 * @Description:
 */
@RestController
@RequestMapping("/ms/api/trainingQuestions")
public class AppTrainingQuestionsController extends BaseController {
    @Autowired
    private MsTrainingQuestionsService msTrainingQuestionService;

    /**
     * 题库管理-问题列表
     *
     * @param user
     * @param request
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated AppTrainingQuestionListParam request,
                       @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                       @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        request.setStartTime(startDate);
        request.setEndTime(endDate);
        logger.info("题库管理-问题列表，loginer:{},AppTrainListParam:{}", user.getUsername(), request.toString());
        PageData<AppTrainingQuestionsDTO> pageData = msTrainingQuestionService.list(request);
        return ResultUtil.success(pageData);
    }

    /**
     * 题库管理- 新增问题
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/question/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppTrainingQuestionAddParam request) {
        msTrainingQuestionService.add(request);
        return ResultUtil.success();
    }


    /**
     * 题库管理-编辑问题
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/question/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppTrainingQuestionEditParam request) {
        msTrainingQuestionService.edit(request);
        return ResultUtil.success();
    }


    /**
     * 题库管理-删除问题
     *
     * @param user
     * @param id
     * @return
     */
    @RequiresPermissions("emp/question/del")
    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("id") Integer id) {
        msTrainingQuestionService.del(id);
        return ResultUtil.success();

    }



}
